cd E:\data

use final_data,clear
global control pgdp pd light gov indsecond1 indthird capital_ratio 
global op = "bdec(3) sdec(3) adjr2 addtext(City FE, Yes, Year FE, Yes)"
/****Table 1: Descriptive statistics***/
tabstat private_pop score_norm $control, s(n me sd min max) f(%6.3f) col(stat)


/******Baseline estimation: Table 2********/
global op = "bdec(3) sdec(3) adjr2 addtext(City FE, Yes, Year FE, Yes)"
qui reghdfe private_pop c.score_norm#c.policy1 , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m1
qui reghdfe private_pop c.score_norm#c.policy1 pgdp pd , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m2
qui reghdfe private_pop c.score_norm#c.policy1  pgdp pd light gov , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m3
qui reghdfe private_pop c.score_norm#c.policy1  $control, absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m4
outreg2 [m1 m2 m3 m4] using baseline_new, word replace $op 


/******Parallel trend test********/

foreach i of numlist 2010/2020{
gen y`i'=(year==`i')
}

/******Placebo test1: Table3 *******/

qui reghdfe private_pop c.score_norm#c.policy1 $control, absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id)  
  est store m1
qui reghdfe private_pop c.score_norm#c.policy1 c.score_norm#c.y2014  $control, absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m2
qui reghdfe private_pop c.score_norm#c.policy1 c.score_norm#c.y2014  c.score_norm#c.y2013  $control, ///
  absorb(i.year i.city_id) cluster(i.year#i.pro_id) 
  est store m3
qui reghdfe private_pop  c.score_norm#c.policy1 c.score_norm#c.y2014 c.score_norm#c.y2013 c.score_norm#c.y2012 ///
 $control , absorb(i.year i.city_id) cluster(i.year#i.pro_id)  
 est store m4
qui reghdfe private_pop  c.score_norm#c.policy1 c.score_norm#c.y2014 c.score_norm#c.y2013 c.score_norm#c.y2012 ///
 c.score_norm#c.y2011 $control , absorb(i.year i.city_id) cluster(i.year#i.pro_id)  
 est store m5
qui reghdfe private_pop  c.score_norm#c.policy1 c.score_norm#c.y2014 c.score_norm#c.y2013 c.score_norm#c.y2012  ///
 c.score_norm#c.y2011 c.score_norm#c.y2010 $control , absorb(i.year i.city_id) cluster(i.year#i.pro_id)   
 est store m6
outreg2 [m1 m2 m3 m4 m5 m6] using Placebo_new, word replace $op 

/******Parallel trend test: Fig 3 *******/
reghdfe private_pop c.score_norm#c.y2010 c.score_norm#c.y2011 c.score_norm#c.y2012 ///
  c.score_norm#c.y2013 c.score_norm#c.y2014 c.score_norm#c.y2016 c.score_norm#c.y2017 c.score_norm#c.y2018 ///
  c.score_norm#c.y2019 c.score_norm#c.y2020  $control ,absorb(i.year i.city_id) cluster(i.year#i.pro_id)  
gen t1 = invttail(3047, 0.025)

gen b_6 = _b[c.score#c.y2010]
gen se_6 = _se[c.score#c.y2010]

gen b_5 = _b[c.score#c.y2011]
gen se_5 = _se[c.score#c.y2011]

gen b_4 = _b[c.score#c.y2012]
gen se_4 = _se[c.score#c.y2012]

gen b_3 = _b[c.score#c.y2013]
gen se_3 = _se[c.score#c.y2013]

gen b_2 = _b[c.score#c.y2014]
gen se_2 = _se[c.score#c.y2014]

gen b0 = _b[c.score#c.y2016]
gen se0 = _se[c.score#c.y2016]

gen b1 = _b[c.score#c.y2017]
gen se1 = _se[c.score#c.y2017]
gen b2 = _b[c.score#c.y2018]
gen se2 = _se[c.score#c.y2018]
gen b3 = _b[c.score#c.y2019]
gen se3 = _se[c.score#c.y2019]
gen b4 = _b[c.score#c.y2020]
gen se4 = _se[c.score#c.y2020]

gen b = .
gen LB= .
gen UB = .

gen aver_b = (b_6+b_5+b_4+b_3+b_2)/5
replace b = 0 if year==2015

replace b = b_6 - aver_b if year==2010
replace b = b_5 - aver_b if year==2011
replace b = b_4 - aver_b if year==2012
replace b = b_3 - aver_b if year==2013
replace b = b_2 - aver_b if year==2014
replace b = b0 - aver_b if year==2016
replace b = b1 - aver_b if year==2017
replace b = b2 - aver_b if year==2018
replace b = b3- aver_b if year==2019
replace b = b4 - aver_b if year==2020

replace LB = b - t1*se_6 if year==2010
replace LB = b - t1*se_5 if year==2011
replace LB = b - t1*se_4 if year==2012
replace LB = b - t1*se_3 if year==2013
replace LB = b - t1*se_2 if year==2014
replace LB = b - t1*se0 if year==2016
replace LB = b - t1*se1 if year==2017
replace LB = b - t1*se2 if year==2018
replace LB = b - t1*se3 if year==2019
replace LB = b - t1*se4 if year==2020

replace UB = b + t1*se_6 if year==2010
replace UB = b + t1*se_5 if year==2011
replace UB = b + t1*se_4 if year==2012
replace UB = b + t1*se_3 if year==2013
replace UB = b + t1*se_2 if year==2014
replace UB = b + t1*se0 if year==2016
replace UB = b + t1*se1 if year==2017
replace UB = b + t1*se2 if year==2018
replace UB = b + t1*se3 if year==2019
replace UB = b + t1*se4 if year==2020
replace b=0 if b==.

keep year b LB UB 
duplicates drop
replace LB=0 if LB==.
replace UB=0 if UB==.
sort year
twoway (connected b year,sort lcolor(black) mcolor(black) msymbol(s) msize(small) cmissing(n) lwidth(thin)) ///
       (rline UB LB year if inrange(year,2010,2020),lwidth(vthin) lpattern(dash) msize(small) lcolor(black)) ///
       ,ytitle("The creation of private firms per 1k population",size(*0.8) margin(r+1)) ///
	    yline(0,lwidth(thin) lpattern(solid) lcolor(teal)) ///
		ylabel(-0.4(0.2)0.8,labsize(small)) ///
		xtitle("",size(small) margin(t+1)) ///
		xline(2015,lwidth(thin) lpattern(solid) lcolor(teal)) ///
		xlabel(2010 "2010" 2011 "2011" 2012 "2012" 2013 "2013" 2014 "2014" 2015 "2015" 2016 "2016" 2017 "2017" 2018   "2018" 2019 "2019" 2020 "2020",labsize(small)) ///		
		legend(off) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
		title("Dynamic effects of digital government",size(*0.8)) ///
		name("whole",replace) ///
		note("Note: Confidence intervals based on 95% level.",size(*0.8))

/******Placebo test 2: Fig 4**********/
use final_data,clear
global control pgdp pd light gov indsecond1 indthird capital_ratio 
gen DID = c.score_norm#c.policy1
permute DID beta = _b[DID], reps(500) seed(100) saving ("Simulation.dta",replace): reghdfe private_pop DID ///
 $control, absorb(i.year i.city_id) cluster(i.year#i.pro_id) 

use Simulation,clear
dpplot beta, xline(0.314, lc(black*0.5) lp(dash)) ///
  xline(0, lc(black*0.5) lp(solid))  ///
  xtitle("Estimator", size(*1))  ///
  xlabel(-0.2(0.1)0.2, labsize(small)) ///
  ytitle("Probability density", size(*1)) ///
  ylabel(, nogrid format(%4.1f) labsize(small)) ///
  note("") caption("") ///
  graphregion(fcolor(white))
graph export "placebo.png", width(1000) replace


/*****Robustness tests*****/ 



/*********Instrument variable: Table 4********/
use final_data,clear
global control pgdp pd light gov indsecond1 indthird capital_ratio 
gen hhid = c.year#c.pro_id

gen internet = c.score_norm#c.policy1
ivreghdfe private_pop  $control (internet=c.office#c.policy1 c.post_task#c.policy1) ///
  , absorb(i.year i.city_id) first cluster(hhid)

/*****Other robustness tests: Table 5******/
//Controlling for province-year level disturbance  
qui reghdfe private_pop c.score_norm#c.policy1  $control, absorb(i.year#i.pro_id i.city_id) ///
  cluster(i.year#i.pro_id)   
  est store m1
//clustering at the city level
qui reghdfe private_pop c.score_norm#c.policy1  $control, absorb(i.year i.city_id) ///
  cluster(i.city_id )   
  est store m2
//generating a dummy

qui reghdfe private_pop c.treat_digit#c.policy1 $control, absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m3
outreg2 [ m1 m2 m3] using other_tests, word replace $op  

 


/******Mechanism analysis: Table 6******/
qui reghdfe private_pop c.score_norm#c.policy1 $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m1
qui reghdfe private_pop c.score_supply#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m2
qui reghdfe private_pop c.score_response#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m3
qui reghdfe private_pop c.score_intell#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m4
outreg2 [m1 m2 m3 m4] using mechanism_new, word replace $op 

/******Mechanism analysis: Table 7******/

qui reghdfe private_pop c.score_norm#c.policy1 c.score_norm#c.policy1#c.item2014  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m1
qui reghdfe private_pop c.score_norm#c.policy1 c.score_norm#c.policy1#c.procedures2014  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id)
  est store m2
outreg2 [m1 m2] using institutions_new, word replace $op   

/*****Heterogeneous firm entry: Table 8**********/
qui reghdfe soe_pop c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id)  
  est store m1
qui reghdfe small_pop c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id)  
  est store m2
qui reghdfe medium_pop c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id)
  est store m3
qui reghdfe large_pop c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id)  
  est store m4
outreg2 [m1 m2 m3 m4] using firm_size_new, word replace $op 

/*****Regional heterogeneous effects: Table 9**********/

reghdfe private_pop c.score_norm#c.policy1#c.high_market c.score_norm#c.policy1#c.low_market $control, ///
  absorb(i.year i.city_id) cluster(i.year#i.pro_id)
scalar b1 = _b[c.score_norm#c.policy1#c.high_market]
scalar se1 = _se[c.score_norm#c.policy1#c.high_market]
scalar b2 = _b[c.score_norm#c.policy1#c.low_market]
scalar se2 = _se[c.score_norm#c.policy1#c.low_market]
scalar diff = (b1-b2)/sqrt(se1^2+se2^2)
scalar p_value = ttail(2750-292, diff)
dis diff p_value
  est store m1
qui reghdfe private_pop c.score_norm#c.policy1#c.high_legal c.score_norm#c.policy1#c.low_legal $control, ///
  absorb(i.year i.city_id)  cluster(i.year#i.pro_id)
scalar b1 = _b[c.score_norm#c.policy1#c.high_legal]
scalar se1 = _se[c.score_norm#c.policy1#c.high_legal]
scalar b2 = _b[c.score_norm#c.policy1#c.low_legal]
scalar se2 = _se[c.score_norm#c.policy1#c.low_legal]
scalar diff = (b1-b2)/sqrt(se1^2+se2^2)
scalar p_value = ttail(2750-292, diff)
dis p_value  
  est store m2
qui reghdfe private_pop c.score_norm#c.policy1#c.high_internet c.score_norm#c.policy1#c.low_internet ///
 $control, absorb(i.year i.city_id) cluster(i.year#i.pro_id)
scalar b1 = _b[c.score_norm#c.policy1#c.high_internet]
scalar se1 = _se[c.score_norm#c.policy1#c.high_internet]
scalar b2 = _b[c.score_norm#c.policy1#c.low_internet]
scalar se2 = _se[c.score_norm#c.policy1#c.low_internet]
scalar diff = (b1-b2)/sqrt(se1^2+se2^2)
scalar p_value = ttail(2750-292, diff)
dis p_value 
  est store m3  
  
outreg2 [m1 m2 m3] using heterogeneity_new, word replace $op     
  
/*********Economic benefits: Table 10**********/
//firm exit: Panel A
qui reghdfe dur2 c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m1
qui reghdfe dur3 c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id)   
  est store m2
qui reghdfe dur4 c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id)  
  est store m3
qui reghdfe dur5 c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id)     
  est store m4
outreg2 [m1 m2 m3 m4] using firm_exit_new, word replace $op   
//Employment and innovation
xtset city_id year
qui reghdfe lnlabor c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id)  
  est store m1
qui reghdfe patenta_pop c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m2
qui reghdfe F.patentg_pop c.score_norm#c.policy1  $control , absorb(i.year i.city_id) ///
  cluster(i.year#i.pro_id) 
  est store m3
outreg2 [m1 m2 m3] using Economicbenefits_new, word replace $op   


  